#include "define.h"
#include "disp.h"
#include "power.h"
#include "clk.h"
#include "gpio.h"
#include "time.h"
#include "uart.h"
#include "spk.h"
#include "flash.h"
#include "adc.h"
#include "algo.h"
#include "bp.h"
#include "func.h"
#include "op.h"
#include "pwm.h"
#include "voice.h"
#include "queue.h"
#include "inner_voice.h"
#include "w25qxx.h"

const uint32_t voice_table[] = {0x0000,0x29df2,0x377be,0x69b76,0x7367c,0x803ba,0xb32c2,0xd4272,0xdc348,0xe3b8a,0xecf9c,0xfb834,0x104eb2,0x10b720,0x10ee50,0x112a26,0x1236f8,0x12a8e0,0x1307d8,0x13bc7c,0x146ba8,0x150540,0x15a76e,0x17efe8,0x1aaa58,0x211a1c,0x2287a6,0x22d9da,0x233358,0x2376f6,0x23c586,0x2420d8,0x24730c,0x24ba50,0x250ab2,0x255eba,0x25b0ee,0x260322,0x265ca0,0x28a352,0x2bf334,0x2f200c};

stru_inner_voice_cmd_regist_t stru_inner_voice_cmd_china_vb044;
uint32_t OP_CR_before = 0;
QUEUE_HandleTypeDef  q_buf;

void OP_Init()
{
    OP_CR_before = OP->OP_CR;

    // OP->OP_CR = 0x00210000;//OPB电压跟随DAO P17输出会与电磁阀干涉 放弃

    OP->OP_CR = 0x00000121; // OPA电压跟随DAO opaps->DA0 opans->A7 opao->A7
}
void OP_stop()
{
    OP->OP_CR = OP_CR_before;

    // OP->OP_CR=0x00000225;//OPA
}
uint32_t BG_CR_before = 0;
uint32_t AP_CR_before = 0;
uint32_t LBT_CR_before = 0;

// 115B功放语音
void DAC_Init()
{
    BG_CR_before = PWR->BG_CR;
    AP_CR_before = PWR->AP_CR;
    LBT_CR_before = LBT->CR;

    PWR->BG_CR |= 0x00008000; // 使能ACM
    PWR->AP_CR |= 0x00010001; // 使能ACM
    PWR->AP_CR |= 0x00000001; // 使能ACM

    // 初始化
    LBT->CR = 0Xc1000000;

    // LCD->SEGR1|=0x01000000;
    LCD->SEGR1 |= 0x02000000;
    LCD->CR |= 0x00000001;
    // LBT->CR2|=0X00010000;	//DAO输出
    LBT->CR2 |= 0X00020000; // AUD输出 AUD_OEN：音频输出使能，使用时需要将 SEG25 使能
}

void DAC_stop()
{
    PWR->BG_CR = BG_CR_before;
    PWR->AP_CR = AP_CR_before;
    LBT->CR = LBT_CR_before;

    // PWR->BG_CR |=0x00008000;//使能ACM
    // PWR->AP_CR |=0x00010001;//使能ACM
    // PWR->AP_CR |=0x00000001;//使能ACM

    // //初始化
    // LBT->CR=0Xc1000000;

    // LCD->SEGR1|=0x01000000;
    LCD->SEGR1 &= ~0x02000000;
    // LCD->CR |= 0x00000001;
    // LBT->CR2|=0X00010000;	//DAO输出
    LBT->CR2 &= ~0X00020000; // AUD输出 AUD_OEN：音频输出使能，使用时需要将 SEG25 使能
}

void Delay_us(int s)
{
    volatile uint32_t i = 3 * s;
    while (i--)
        ;
}

uint8_t inner_voice_key_exit(void)
{
    uint8_t res = 0;
    // if (!KEY_POWER || !KEY_MEMORYUP || !KEY_TIME)
    // {
    //     res = 1;
    // }
    if (!KEY_POWERIO || !KEY_MEMORYUPIO || !KEY_TIMEIO)
    {
        res = 1;
        VoiceStop = 1;
    	if(!KEY_POWERIO) KEY_POWER=0;//20220420EMC
    	if(!KEY_TIMEIO) KEY_TIME=0;
    	if(!KEY_MEMORYUPIO) KEY_MEMORYUP=0;

    }

    return res;
}
// #define TIMER1_LOADVAL_NORMAL_SPEED (65535-750) // 24000000 / 4 * 0.000125   \8000hz
// #define TIMER1_LOADVAL_ACCELER_SPEED (65535-60) // 24000000 / 4 * 0.000010   \8000hz
#define TIMER1_LOADVAL_NORMAL_SPEED (65535-372) // 24000000 / 4 * 0.000062   \16000HZ
#define TIMER1_LOADVAL_ACCELER_SPEED (65535-60) // 24000000 / 4 * 0.000005   \16000HZ
// #define TIMER1_LOADVAL_NORMAL_SPEED  0xfa23
void user_timer1_init(void)
{
	//RCC_APB1PeriphClockConfig(RCC_APBPeriph_TM0,RCC_TMCLK_IHRC,RCC_TMCLK_DIV128);//RCC_TMCLK_DIV8
	RCC_APB1PeriphClockConfig(RCC_APBPeriph_TM1,RCC_TMCLK_IHRC,RCC_TMCLK_DIV4);
	RCC_APBPeriphClockCmd(RCC_APBPeriph_TM1, ENABLE);   //外设时钟使能
	TM_SetAutoReload(TM1,TIMER1_LOADVAL_NORMAL_SPEED);
	TM_Cmd(TM1, ENABLE);
	//TM_PFDCmd(ENABLE);
	TM_ITCmd(TM1,ENABLE);
	TM_ClearITStatus(TM1);
	VIC_IRQChannelCmd(TM1_IRQn,ENABLE);                            //允许中断请求

	VIC_InitTypeDef  VIC_InitStruct;
	RCC_APBPeriphClockCmd(RCC_APBPeriph_TM1, ENABLE);      //使能外部中断和按键中断模块的时钟

	VIC_InitStruct.VIC_IRQChannel=TM1_IRQn;
	VIC_InitStruct.VIC_IRQChannelPriority=VIC_IRQChannelPriority0;
	VIC_InitStruct.VIC_PrioThreshold=VIC_PrioThreshold0;
	VIC_InitStruct.VIC_PrioThresholdCmd=ENABLE;
	VIC_InitStruct.VIC_IRQChannelWakeUpLowPowerCmd=ENABLE;
	VIC_InitStruct.VIC_IRQChannelCmd=ENABLE;

	VIC_Init(&VIC_InitStruct);
}

void voice_acceler_speed_pro(void)
{
	TM_SetAutoReload(TM1,TIMER1_LOADVAL_ACCELER_SPEED);
}

void voice_normal_speed_pro(void)
{
	TM_SetAutoReload(TM1,TIMER1_LOADVAL_NORMAL_SPEED);
}

uint8_t flag_buf_to_dac_done = 0;
uint8_t voice_step = 0;
uint8_t flag_voice_start_disp = 0;
uint32_t start_addr; 
uint32_t stop_addr;
void read_wq25qxx_to_qbuf_pro(uint32_t read_addr,uint32_t push_cnt)
{
    volatile uint16_t temp_buf[4];
    QUEUE_StatusTypeDef q_res = QUEUE_OK;
    for (uint16_t i = 0; i < push_cnt; i+= 2)
    {
        W25QXX_Read((uint8_t *)&temp_buf[0], read_addr + i, 2);
        if ((temp_buf[0] & 0x8000) == 0x8000)
        {
            temp_buf[1] = (temp_buf[0] - 32768) / 16;
        }
        else
        {
            temp_buf[1] = (temp_buf[0] + 32768) / 16;
        }
        q_res = Queue_Push(&q_buf,temp_buf[1]);
        if (q_res != QUEUE_OK)
        {
            break;
        }
    }
}


void it_buf_to_dac_init(uint32_t start_addr_in, uint32_t stop_addr_in)
{
    flag_buf_to_dac_done = 0;
    voice_step = 0;
    start_addr = start_addr_in;
    stop_addr = stop_addr_in;
    flag_voice_start_disp = 1;
    voice_normal_speed_pro();
    Queue_Clear(&q_buf);
}

void it_buf_to_dac_deinit(void)
{
    flag_buf_to_dac_done = 0;
    flag_buf_to_dac_done = 0;
    flag_voice_start_disp = 0;

    Queue_Clear(&q_buf);
}

// #define INNER_VOICE_OFFSET_HIGHT (2048 + 50)   // 过滤空闲音频 进行加速处理的空闲音频信号 过滤高值
// #define INNER_VOICE_OFFSET_LOW (2048 - 50)     // 过滤低值
// #define INNER_VOICE_ACCELERATE_COUNT_MAX (200) // 过滤次数
#define INNER_VOICE_ACCELERATE_TIME_VAL (15)   // 加速时的时间
#define INNER_VOICE_NALMAL_TIME_VAL (89)       // (89)正常播报速度时的时间

#define INNER_VOICE_OFFSET_HIGHT (2048 + 50)   // 过滤空闲音频 进行加速处理的空闲音频信号 过滤高值
#define INNER_VOICE_OFFSET_LOW (2048 - 50)     // 过滤低值
#define INNER_VOICE_ACCELERATE_COUNT_MAX (200 * 2) // 过滤次数

void it_buf_to_dac_pro(void)
{
    static volatile uint16_t SPIReciveData[4];
    static volatile uint32_t read_addr;
    static volatile uint16_t inner_voice_accelerate_count = 0;
    static volatile uint16_t delay_time = 0;
    static volatile uint8_t flag_is_accelerate_to_end = 0; // 进行加速直到结束
    static volatile uint32_t accelerate_start_addr;        // 大于一定地址时才进行加速到结束  作用是消除每段语音后面可能存在的杂音
    static volatile uint32_t total_addr;

    if (flag_voice_start_disp == 0)
    {
        return;
    }
    

    if (voice_step == 0)
    {
        voice_step++;
        flag_buf_to_dac_done = 0;
        read_addr = start_addr;
        total_addr = (stop_addr - start_addr);
        accelerate_start_addr = (stop_addr - (total_addr / 10));
        inner_voice_accelerate_count = 0;
        delay_time = 0;
        flag_is_accelerate_to_end = 0;
    }
    else if (voice_step == 1)
    {
        read_addr+=2;
        if (read_addr < stop_addr)
        {
            Queue_Pop(&q_buf,(QUEUE_DATA_T *)&SPIReciveData[1]);


            if (SPIReciveData[1] < INNER_VOICE_OFFSET_HIGHT && SPIReciveData[1] > INNER_VOICE_OFFSET_LOW)
            {
                inner_voice_accelerate_count++;
                if (inner_voice_accelerate_count > INNER_VOICE_ACCELERATE_COUNT_MAX)
                {
                    inner_voice_accelerate_count = 0;
                    // delay_time = INNER_VOICE_ACCELERATE_TIME_VAL;
                    if (read_addr > accelerate_start_addr)
                    {
                        #if 00

                        #else
                        flag_is_accelerate_to_end = 1; // 一直加速直到结束
                        voice_acceler_speed_pro();
                        #endif
                    }
                }
            }
            else
            {
                inner_voice_accelerate_count = 0;
                delay_time = INNER_VOICE_NALMAL_TIME_VAL;
            }
            // *(uint32_t*)0x4001E00C =0x00008000+(SPIReciveData[1]);

            if (flag_is_accelerate_to_end == 0) //
            {
                LBT->CR2 = (1 << 17) | SPIReciveData[1];
            }
            else
            {
                LBT->CR2 = (1 << 17) | 2048; // 加速到结束时一直把音频信号归置到零点 12bitdac / 2
            }


        }
        else
        {
            flag_buf_to_dac_done = 1;
            flag_voice_start_disp = 0;
        }
    }
}


void AUD_Voice_Data(uint32_t start_addr, uint32_t stop_addr)
{
    SHUTDOWN_PIN_HIGHT;
    read_wq25qxx_to_qbuf_pro(start_addr,2048);
    start_addr += 2048;
    it_buf_to_dac_init(start_addr,stop_addr);
    while (1)
    {
        if (Queue_Count(&q_buf) < 2048 - 128)
        {
            if (stop_addr - start_addr >= 128)
            {
                read_wq25qxx_to_qbuf_pro(start_addr,128);
                start_addr += 128;
            }
            else
            {
                for (uint16_t i = 0; i < 128; i++)
                {
                    QUEUE_StatusTypeDef q_res = Queue_Push(&q_buf,2048);
                    if (q_res != QUEUE_OK)
                    {
                        break;
                    }
                }
                start_addr += 128;
            }
        }
        
        if (flag_buf_to_dac_done == 1)
        {
            goto INNER_VOICE_EXIT;
        }
        
        if (inner_voice_key_exit() == 1)
        {
            VoiceStop = 1;
            goto INNER_VOICE_EXIT;
        }
    }
    
INNER_VOICE_EXIT:
    it_buf_to_dac_deinit();
    SHUTDOWN_PIN_LOW;
}


// void AUD_Voice_Data(uint32_t start_addr, uint32_t stop_addr)
// {
//     volatile uint16_t SPIReciveData[4];
//     volatile uint32_t j;
//     volatile uint16_t inner_voice_accelerate_count = 0;
//     volatile uint16_t delay_time = 0;
//     volatile uint8_t flag_is_accelerate_to_end = 0; // 进行加速直到结束
//     volatile uint32_t accelerate_start_addr;        // 大于一定地址时才进行加速到结束  作用是消除每段语音后面可能存在的杂音
//     volatile uint32_t total_addr;

//     total_addr = (stop_addr - start_addr);
//     accelerate_start_addr = (stop_addr - (total_addr / 10)) / 2;

//     SHUTDOWN_PIN_HIGHT;
//     for (j = start_addr / 2; j < stop_addr / 2; j++)
//     {
//         W25QXX_Read((uint8_t *)&SPIReciveData, j * 2, 2);
//         if ((SPIReciveData[0] & 0x8000) == 0x8000)
//         {
//             SPIReciveData[1] = (SPIReciveData[0] - 32768) / 16;
//        }
//         else
//         {
//             SPIReciveData[1] = (SPIReciveData[0] + 32768) / 16;
//         }

//         if (SPIReciveData[1] < INNER_VOICE_OFFSET_HIGHT && SPIReciveData[1] > INNER_VOICE_OFFSET_LOW)
//         {
//             inner_voice_accelerate_count++;
//             if (inner_voice_accelerate_count > INNER_VOICE_ACCELERATE_COUNT_MAX)
//             {
//                 inner_voice_accelerate_count = 0;
//                 delay_time = INNER_VOICE_ACCELERATE_TIME_VAL;
//                 if (j > accelerate_start_addr)
//                 {
//                     flag_is_accelerate_to_end = 1; // 一直加速直到结束
//                 }
//             }
//         }
//         else
//         {
//             inner_voice_accelerate_count = 0;
//             delay_time = INNER_VOICE_NALMAL_TIME_VAL;
//         }
//         // *(uint32_t*)0x4001E00C =0x00008000+(SPIReciveData[1]);

//         if (flag_is_accelerate_to_end == 0) //
//         {
//             LBT->CR2 = (1 << 17) | SPIReciveData[1];
//         }
//         else
//         {
//             LBT->CR2 = (1 << 17) | 2048; // 加速到结束时一直把音频信号归置到零点 12bitdac / 2
//         }

//         // LBT->CR2 |= 1 << 17;

//         // Delay_us(35); //16000 采样
//         // Delay_us(89);//8000 采样
//         Delay_us(delay_time); // 8000 采样

//         if (inner_voice_key_exit() == 1)
//         {
//             VoiceStop = 1;
//             goto INNER_VOICE_EXIT;
//         }
//     }
// INNER_VOICE_EXIT:
//     SHUTDOWN_PIN_LOW;
// }

void inner_voice_gpio_init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // shutdown pin init
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_PTSR = GPIO_PTSR_STRONG;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIO4, &GPIO_InitStructure);
    SHUTDOWN_PIN_LOW;

	// GPIO_WriteOutputDataBit(GPIO2,GPIO_Pin_7,Bit_SET);//d:SPI FLASH不选中
	// GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
	// GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
	// GPIO_InitStructure.GPIO_PTSR=GPIO_PTSR_WEAK;
	// GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
	// GPIO_Init(GPIO2,&GPIO_InitStructure);
}

void inner_voice_init(void)
{
    W25QXX_Init();
    OP_Init();
    DAC_Init();
    user_timer1_init();

    inner_voice_gpio_init();

    RCC_SYSCLKConfig(RCC_SYSCLK_IHRC, RCC_SYSCLK_DIV1); // sys选择内部高速24MHz时钟

    stru_inner_voice_cmd_china_vb044.inner_voice_cmd_number_offset = INNER_CMD_ZERO;
    stru_inner_voice_cmd_china_vb044.inner_voice_cmd_ten = INNER_CMD_TEN;
    stru_inner_voice_cmd_china_vb044.inner_voice_cmd_hundred = INNER_CMD_HUNDRED;
    stru_inner_voice_cmd_china_vb044.inner_voice_cmd_time = INNER_CMD_TIMES;
    stru_inner_voice_cmd_china_vb044.inner_voice_cmd_spot = INNER_CMD_SPOT;
    stru_inner_voice_cmd_china_vb044.inner_voice_cmd_mm_hg = INNER_CMD_MILLIMETRE_OF_MERCURY;
    stru_inner_voice_cmd_china_vb044.inner_voice_cmd_kilopascal = INNER_CMD_KILOPASCAL;

    Queue_Init(&qVoice, &voice_buffer[0], Q_VOICE_BUFFER_SIZE);

    Queue_Init(&q_buf, (uint16_t *)&MemBack[0], 2048);

}
void inner_voice_stop(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    OP_stop();
    DAC_stop();
    SPI_DeInit();

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // shutdown pin init
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_PTSR = GPIO_PTSR_STRONG;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIO4, &GPIO_InitStructure);

    SHUTDOWN_PIN_LOW;

    RCC_SetSYSCLKDefault(); // 1：选择过渡时钟作为系统时钟 (默认项)；24MHz / 2 == 12mhz
    UserOPA_Init(); //播报结束后需要将opa重新初始化, 因为dac播报改变了opa的配置
}

// 根据建表voice_table进行播报
// voice_position  1-- 表的最大元素
void inner_voice_table_play(e_inner_voice_cmd_typedef voice_position)
{
    uint16_t voice_table_max = 0;
    uint32_t voice_table_start_addr = 0;
    uint32_t voice_table_end_addr = 0;

    voice_table_max = sizeof(voice_table) / sizeof(voice_table[0]); //

    if (voice_position > voice_table_max || voice_position == 0)
    {
        return;
    }

    voice_table_start_addr = voice_table[voice_position - 1];
    voice_table_end_addr = voice_table[voice_position];

    VoiceStop = 0;
    AUD_Voice_Data(voice_table_start_addr, voice_table_end_addr);
}

// 语音处理分割数字百十个位
// inner_voice_cmd 注册的数字以及次数等指令偏移量, 用于适配不同语音列表
//  e_pres_unit 选择当前分割的数字类型 e_inner_voice_pres_unit_t 中定义
//  number 要进行分割的数字
void inner_voice_devide_number_pro(stru_inner_voice_cmd_regist_t inner_voice_cmd, e_inner_voice_pres_unit_t e_pres_unit, u16 number)
{
    u8 temp_hundred = 0;
    u8 temp_ten = 0;
    u8 temp_single = 0;
    temp_hundred = number / 100;
    temp_ten = number % 100 / 10;
    temp_single = number % 10;
    if (e_pres_unit == INNER_VOICE_PRES_UNIT_MMG || e_pres_unit == INNER_VOICE_PRES_UNIT_HEARTBEAT) // mmg和心跳次数处理
    {
        if (temp_hundred)
        {
            temp_hundred += inner_voice_cmd.inner_voice_cmd_number_offset;
            Queue_Push(&qVoice, temp_hundred);                            // 入列
            Queue_Push(&qVoice, inner_voice_cmd.inner_voice_cmd_hundred); // 入列
        }
        if (temp_ten)
        {
            temp_ten += inner_voice_cmd.inner_voice_cmd_number_offset;
            Queue_Push(&qVoice, temp_ten);                            // 入列
            Queue_Push(&qVoice, inner_voice_cmd.inner_voice_cmd_ten); // 入列
        }
        else // 十位没有
        {
            if (temp_hundred && temp_single) // 如果百位和个位都有效 十位需要加一个"零"
            {
                Queue_Push(&qVoice, 0 + inner_voice_cmd.inner_voice_cmd_number_offset); // 入列
            }
        }

        if (temp_single)
        {
            temp_single += inner_voice_cmd.inner_voice_cmd_number_offset;
            Queue_Push(&qVoice, temp_single); // 入列
        }
        else if (e_pres_unit == INNER_VOICE_PRES_UNIT_HEARTBEAT && (!temp_hundred && !temp_ten && !temp_single)) // 心跳显示HI和LOW时传入的值为0, 此时播报"零次"
        {
            Queue_Push(&qVoice, 0 + inner_voice_cmd.inner_voice_cmd_number_offset); // 入列
        }

        if (e_pres_unit == INNER_VOICE_PRES_UNIT_MMG) // mmg和心跳次数处理
        {
            Queue_Push(&qVoice, inner_voice_cmd.inner_voice_cmd_mm_hg); // 入列
        }
        else if (e_pres_unit == INNER_VOICE_PRES_UNIT_HEARTBEAT) // mmg和心跳次数处理
        {
            Queue_Push(&qVoice, inner_voice_cmd.inner_voice_cmd_time); // 入列
        }
    }
    else // kpa处理
    {
        if (temp_hundred) // 实际上是十位 10倍数
        {
            if (temp_hundred == 1) // 为1时 只播报"十"
            {
                Queue_Push(&qVoice, inner_voice_cmd.inner_voice_cmd_ten); // 入列
            }
            else
            {
                temp_hundred += inner_voice_cmd.inner_voice_cmd_number_offset;
                Queue_Push(&qVoice, temp_hundred);                        // 入列
                Queue_Push(&qVoice, inner_voice_cmd.inner_voice_cmd_ten); // 入列
            }
        }
        if (temp_ten) // 实际上是个位 10倍数
        {
            temp_ten += inner_voice_cmd.inner_voice_cmd_number_offset;
            Queue_Push(&qVoice, temp_ten); // 入列
        }

        if (temp_single) // 实际上是小数位 10倍数
        {
            Queue_Push(&qVoice, inner_voice_cmd.inner_voice_cmd_spot); // 如果小数位有效 需要加一个"点"
            temp_single += inner_voice_cmd.inner_voice_cmd_number_offset;
            Queue_Push(&qVoice, temp_single); // 入列
        }
        Queue_Push(&qVoice, inner_voice_cmd.inner_voice_cmd_kilopascal); // 入列
    }
}

// 内置语音发送组合cmd服务函数
void inner_voice_send_combine_cmd_server(void)
{
    u16 voice_temp = 0; // 取队列数据
	TM_ITCmd(TM0,DISABLE);
    while (1)
    {
        if (Queue_Count(&qVoice) > 0)
        {
            if (Queue_Pop(&qVoice, &voice_temp) == QUEUE_OK)
            {
                inner_voice_table_play(voice_temp);
                if (VoiceStop)
                {
                    Queue_Clear(&qVoice);
                    break;
                }
            }
        }
        else
        {
            Queue_Clear(&qVoice);
            break;
        }
    }
    UserTimer0_Init();
}

// 往语音队列中添加一个指令
void inner_voice_add_cmd(u8 e_voice_cmd)
{
    Queue_Push(&qVoice, e_voice_cmd); // 入列
}

// 高压是+SYS+毫米汞柱/kpa+低压是+DIA+毫米汞柱/kpa+每分钟心跳次数是+PUL+次
// 组合语音处理函数
//  e_pres_unit 值:     e_inner_voice_pres_unit_t 中定义
//  sys 值:     血压高压 0~255 只需输入mmg \kpa由函数内部自动转换
//  dia 值:     血压低压 0~255
//  pul 值:     每分钟心跳 0~255
void inner_voice_combine_cmd(e_inner_voice_pres_unit_t e_pres_unit, u8 sys, u8 dia, u8 pul)
{
    u16 temp_sys = 0;
    u16 temp_dia = 0;
    temp_sys = sys;
    temp_dia = dia;
    if (e_pres_unit == INNER_VOICE_PRES_UNIT_KPA) // mmg -> kpa转换
    {
        temp_sys *= 4;
        temp_sys /= 3;
        temp_dia *= 4;
        temp_dia /= 3;
    }

    Queue_Push(&qVoice, INNER_CMD_HIGH_PRESSURE_IS); // 入列
    inner_voice_devide_number_pro(stru_inner_voice_cmd_china_vb044, e_pres_unit, temp_sys);

    Queue_Push(&qVoice, INNER_CMD_LOW_PRESSURE_IS); // 入列
    inner_voice_devide_number_pro(stru_inner_voice_cmd_china_vb044, e_pres_unit, temp_dia);

    Queue_Push(&qVoice, INNER_CMD_NUMBER_HEARTBEATS_PER_MINUTE_IS); // 入列
    inner_voice_devide_number_pro(stru_inner_voice_cmd_china_vb044, INNER_VOICE_PRES_UNIT_HEARTBEAT, pul);
}

// 组合播报
void inner_voice_combine_cmd_pro(e_inner_voice_pres_unit_t e_pres_unit, u8 sys, u8 dia, u8 pul)
{
    inner_voice_init();
    Queue_Clear(&qVoice);
    inner_voice_combine_cmd(e_pres_unit, sys, dia, pul);
    inner_voice_send_combine_cmd_server();
    inner_voice_stop();
}
// 单句播报
void inner_voice_one_cmd(uint16_t inner_voice_cmd)
{
	TM_ITCmd(TM0,DISABLE);
    inner_voice_init();
    inner_voice_table_play(inner_voice_cmd);
    inner_voice_stop();
    UserTimer0_Init();
}

void inner_voice_who(u8 wholevel)
{
    inner_voice_init();
    Queue_Clear(&qVoice);
    inner_voice_add_cmd(INNER_CMD_ACCORDING_TO_WORLD_HEALTH_ORGANIZATION_STANDARDS); //
    inner_voice_add_cmd(INNER_CMD_YOUR_BLOOD_PRESSURE);                              // 增加插入一条语音 "您的血压"
    if (wholevel == 1)                                                               // 根据who等级选择增加一条语音 "正常" "
    {
        inner_voice_add_cmd(INNER_CMD_NORMAL);
    }
    else if (wholevel == 2)
    {
        inner_voice_add_cmd(INNER_CMD_NORMAL_TO_HIGH);
    }
    else if (wholevel == 3)
    {
        inner_voice_add_cmd(INNER_CMD_MILD_HYPERTENSION);
    }
    else if (wholevel == 4)
    {
        inner_voice_add_cmd(INNER_CMD_MODERATE_HYPERTENSION);
    }
    else if (wholevel == 5)
    {
        inner_voice_add_cmd(INNER_CMD_SEVERE_HYPERTENSION);
    }
    inner_voice_send_combine_cmd_server();
    inner_voice_stop();
}
